﻿namespace UnityEditor.Graphs.AnimationStateMachine
{
    using System;
    using UnityEditor;
    using UnityEditor.Graphs;
    using UnityEditorInternal;
    using UnityEngine;

    internal class StateTransitionsEditor
    {
        private AnimatorController m_Controller;
        private Editor m_Host;
        private bool m_LockList;
        private UnityEditorInternal.State m_State;
        private TransitionInspector m_TransitionInspector;
        private ReorderableList m_TransitionList;
        private Transition[] m_Transitions;
        public static System.Action stateDirtyCallback;

        public StateTransitionsEditor(UnityEditorInternal.State state, Editor host)
        {
            this.m_Host = host;
            this.m_State = state;
        }

        public bool HasPreviewGUI()
        {
            return ((this.m_TransitionInspector != null) && this.m_TransitionInspector.HasPreviewGUI());
        }

        public void OnDestroy()
        {
            UnityEngine.Object.DestroyImmediate(this.m_TransitionInspector);
        }

        public void OnDisable()
        {
            AnimatorControllerTool.graphDirtyCallback = (System.Action) Delegate.Remove(AnimatorControllerTool.graphDirtyCallback, new System.Action(this.OnGraphDirty));
            this.m_Controller.OnAnimatorControllerDirty = (System.Action) Delegate.Remove(this.m_Controller.OnAnimatorControllerDirty, new System.Action(this.ResetTransitionList));
        }

        public void OnEnable()
        {
            this.ResetTransitionList();
            AnimatorControllerTool.graphDirtyCallback = (System.Action) Delegate.Combine(AnimatorControllerTool.graphDirtyCallback, new System.Action(this.OnGraphDirty));
            this.m_Controller = AnimatorControllerTool.tool.animatorController;
            this.m_Controller.OnAnimatorControllerDirty = (System.Action) Delegate.Combine(this.m_Controller.OnAnimatorControllerDirty, new System.Action(this.ResetTransitionList));
        }

        private void OnGraphDirty()
        {
            this.ResetTransitionList();
        }

        public void OnInspectorGUI()
        {
            EditorGUI.BeginChangeCheck();
            this.m_TransitionList.DoList();
            if (EditorGUI.EndChangeCheck() && (stateDirtyCallback != null))
            {
                stateDirtyCallback();
                GUIUtility.ExitGUI();
            }
            if (this.m_TransitionInspector != null)
            {
                this.m_TransitionInspector.OnInspectorGUI();
            }
        }

        public void OnInteractivePreviewGUI(Rect r, GUIStyle background)
        {
            this.m_TransitionInspector.OnInteractivePreviewGUI(r, background);
        }

        public void OnPreviewSettings()
        {
            this.m_TransitionInspector.OnPreviewSettings();
        }

        private void OnTransitionElement(Rect rect, int index, bool selected, bool focused)
        {
            TransitionInspector.DrawTransitionElementCommon(rect, this.m_Transitions[index], selected, focused);
        }

        private void RemoveTransition(ReorderableList transitionList)
        {
            UnityEngine.Object.DestroyImmediate(this.m_TransitionInspector);
            this.rootStateMachine.RemoveTransition(this.m_Transitions[this.m_TransitionList.index]);
            this.ResetTransitionList();
        }

        private void ReorderTransition(ReorderableList list)
        {
            this.m_LockList = true;
            Undo.RegisterUndo(this.rootStateMachine, "Reorder transition");
            this.rootStateMachine.SetTransitionsFromState(this.m_State, this.m_Transitions);
            this.m_LockList = false;
        }

        private void ResetTransitionList()
        {
            if (!this.m_LockList)
            {
                this.m_Transitions = this.rootStateMachine.GetTransitionsFromState(this.m_State);
                this.m_TransitionList = new ReorderableList(this.m_Transitions, typeof(Transition), true, true, false, true);
                this.m_TransitionList.drawHeaderCallback = new ReorderableList.HeaderCallbackDelegate(TransitionInspector.DrawTransitionHeaderCommon);
                this.m_TransitionList.drawElementCallback = new ReorderableList.ElementCallbackDelegate(this.OnTransitionElement);
                this.m_TransitionList.onSelectCallback = new ReorderableList.SelectCallbackDelegate(this.SelectTransition);
                this.m_TransitionList.onReorderCallback = new ReorderableList.ReorderCallbackDelegate(this.ReorderTransition);
                this.m_TransitionList.onRemoveCallback = new ReorderableList.RemoveCallbackDelegate(this.RemoveTransition);
                this.m_TransitionList.displayAdd = false;
                this.m_Host.Repaint();
            }
        }

        private void SelectTransition(ReorderableList list)
        {
            Transition transition = this.m_Transitions[list.index];
            this.m_TransitionInspector = Editor.CreateEditor(transition) as TransitionInspector;
            this.m_TransitionInspector.showTransitionList = false;
        }

        private StateMachine rootStateMachine
        {
            get
            {
                return AnimatorControllerTool.tool.animatorController.GetLayerStateMachine(AnimatorController.lastActiveLayerIndex);
            }
        }
    }
}

